<?php
namespace Core\Widget;

use Model\Factory\TableGatewayFactory;

class WidgetHtml extends  WidgetAbstract{

    public function form()
    {
        $html = '<div id="widgetContainer"><table cellspacing="5px" cellpadding="5px">
        <td>Tên block(*): </td>
        <td><input id="txtBlockName" name="txtBlockName" class="k-textbox" style="width: 250px" required placeholder="Nhập tên, bắt buộc" validationMessage="Chưa nhập tên">
            <span class="k-invalid-msg" data-for="txtBlockName"></span>
        </td>
        </tr>
        <tr>
        <td>Html(*): </td>
        <td><textarea id="txtHtml" name="txtHtml" class="k-textbox" style="width: 350px" required placeholder="Nhập html muốn hiển thị, bắt buộc" validationMessage="Chưa nhập html"></textarea>
            <span class="k-invalid-msg" data-for="txtHtml"></span>
        </td>
        </tr>
        <tr>
        <td>Css Class: </td>
        <td><input id="txtClass" class="k-textbox" style="width: 350px" placeholder="foo_class bar_class"></td>
        </tr>
        <tr>
        <td>Css Style: </td>
        <td><input id="txtStyle" class="k-textbox" style="width: 350px" placeholder="width:300px; color:red"></td>
        </tr>
        <tr>
        </table>
        </div>';
        $javascript = '<script type="text/javascript">
            widgetValidator = $("#widgetContainer").kendoValidator().data("kendoValidator");
            function validateWidgetForm(){
                 return widgetValidator.validate();
            }
            function doAddWidget(){
                if(!validateWidgetForm()){
                    return;
                }
                blockName = $("#txtBlockName").val();
                txtHtml = $("#txtHtml").val();
                txtClass = $("#txtClass").val();
                txtStyle = $("#txtStyle").val();
                widgetName = "'.$this->getProgramCode().'";
                resultArr = new Array("blockName");
                resultArr["blockName"] = blockName;
                resultArr["text"] = txtHtml;
                resultArr["class"] = txtClass;
                resultArr["style"] = txtStyle;
                resultArr["widgetName"] = widgetName;

                return {name:blockName, html:txtHtml, class:txtClass, style:txtStyle, widgetName:"'.$this->getProgramCode().'"};

            }
        </script>';
        return $html.$javascript;
    }

    public function render($blockInstanceItem)
    {
        $style = $blockInstanceItem->wrap_style;
        $class = $blockInstanceItem->wrap_class;
        $params = $blockInstanceItem->params;
        $paramArr = $this->getArrayFromJson($params);
        $html = $paramArr['html'];
        $html = '<div class="'.$class.'" style="'.$style.'">'.$html.'</div>';
        return $html;
    }

    public function save($allParams)
    {
        $html = $allParams['html'];
        $name = $allParams['name'];
        $class = $allParams['class'];
        $style = $allParams['style'];
        $widgetName = $allParams['widgetName'];

        $params = $this->buildJSONString(array('html'=>$html));
        $blockClass = TableGatewayFactory::getBlockClassTable();

        $blockClass_Item = $blockClass->findByProgramCode($widgetName);
        $blockInstance = TableGatewayFactory::getBlockInstanceTable();

        if($allParams['widgetId']){
            $blockInstance->update(array('block_class_id'=>$blockClass_Item->id,
                    'name'=>$name, 'params'=>$params, 'wrap_class'=>$class,
                    'wrap_style'=>$style),
                $allParams['widgetId']);
            return $allParams['widgetId'] ;
        }

        else{
            $id = $blockInstance->insert(array(
                'block_class_id'=>$blockClass_Item->id,
                'name'=>$name, 'params'=>$params, 'wrap_class'=>$class, 'wrap_style'=>$style));
            return $id;
        }


    }

    public function form_update($blockInstance_Item, $blockClass_Item)
    {

        $paramArr = $this->getArrayFromJson($blockInstance_Item->params);
        $html = '<h3>'.$blockClass_Item->name.'</h3>
        <input id="txtWidgetId" type="hidden" value="'.$blockInstance_Item->id.'">
        <div id="widgetContainer"><table cellspacing="5px" cellpadding="5px">
        <td>Tên block(*): </td>
        <td><input value="'.$blockInstance_Item->name.'" id="txtBlockName" name="txtBlockName" class="k-textbox" style="width: 250px" required placeholder="Nhập tên, bắt buộc" validationMessage="Chưa nhập tên">
            <span class="k-invalid-msg" data-for="txtBlockName"></span>
        </td>
        </tr>
        <tr>
        <td>Text(*): </td>
        <td><textarea  id="txtHtml" name="txtHtml" class="k-textbox" style="width: 350px" required placeholder="Nhập html muốn hiển thị, bắt buộc" validationMessage="Chưa nhập text">'.$paramArr['html'].'</textarea>
            <span class="k-invalid-msg" data-for="txtHtml"></span>
        </td>
        </tr>
        <tr>
        <td>Css Class: </td>
        <td><input value="'.$blockInstance_Item->wrap_class.'" id="txtClass" class="k-textbox" style="width: 350px" placeholder="foo_class bar_class"></td>
        </tr>
        <tr>
        <td>Css Style: </td>
        <td><input value="'.$blockInstance_Item->wrap_style.'" id="txtStyle" class="k-textbox" style="width: 350px" placeholder="width:300px; color:red"></td>
        </tr>
        <tr>
        </table>
        </div>';
        $javascript = '<script type="text/javascript">
            widgetValidator = $("#widgetContainer").kendoValidator().data("kendoValidator");
            function validateWidgetForm(){
                 return widgetValidator.validate();
            }
            function doUpdateWidget(){

                if(!validateWidgetForm()){
                    return;
                }

                blockName = $("#txtBlockName").val();
                txtHtml = $("#txtHtml").val();
                txtClass = $("#txtClass").val();
                txtStyle = $("#txtStyle").val();
                widgetName = "'.$this->getProgramCode().'";
                txtWidgetId = $("#txtWidgetId").val();


                return {name:blockName, html:txtHtml, class:txtClass, style:txtStyle,
                widgetName:"'.$this->getProgramCode().'",widgetId:txtWidgetId};

            }
        </script>';
        return $html.$javascript;
    }

    public function preview($allParams)
    {
        $style = $allParams['style'];
        $class = $allParams['class'];
        $html = $allParams['html'];
        $html = '<div class="'.$class.'" style="'.$style.'">'.$html.'</div>';
        return $html;
    }

    public function form_content($blockInstance_Item)
    {
    }

    public function saveContent($allParams)
    {
        // TODO: Implement saveContent() method.
    }

    public function getProgramCode()
    {
        return 'Html';
    }
}